<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Mux Stream Viewer</title>
    <script src="https://cdn.jsdelivr.net/npm/hls.js@latest"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        #playbackIdInput {
            margin: 10px 0;
            padding: 5px;
            width: 300px;
        }
        #loadStream {
            padding: 5px 10px;
            cursor: pointer;
        }
        #videoContainer {
            margin-top: 20px;
        }
        video {
            max-width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <h1>Mux Stream Viewer</h1>
    <input type="text" id="playbackIdInput" placeholder="Enter Playback ID">
    <button id="loadStream">Load Stream</button>
    <div id="videoContainer">
        <video id="video" controls></video>
    </div>

    <script>
        const video = document.getElementById('video');
        const playbackIdInput = document.getElementById('playbackIdInput');
        const loadStreamButton = document.getElementById('loadStream');

        loadStreamButton.addEventListener('click', loadStream);

        function loadStream() {
            const playbackId = playbackIdInput.value.trim();
            if (!playbackId) {
                alert('Please enter a Playback ID');
                return;
            }

            const sourceUrl = `https://stream.mux.com/${playbackId}.m3u8`;

            if (Hls.isSupported()) {
                const hls = new Hls();
                hls.loadSource(sourceUrl);
                hls.attachMedia(video);
                hls.on(Hls.Events.MANIFEST_PARSED, function() {
                    video.play();
                });
            } else if (video.canPlayType('application/vnd.apple.mpegurl')) {
                video.src = sourceUrl;
                video.addEventListener('loadedmetadata', function() {
                    video.play();
                });
            } else {
                alert('HLS is not supported in your browser');
            }
        }
    </script>
</body>
</html>